﻿using IH.Comm.Constant;
using IH.Comm.Dtos;
using IH.Comm.Dtos.Users;
using IH.Storage.Entity;
using MongoDB.Driver;
using NAutowired.Core.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IH.Service
{
    [Service]
    public class UserService : CoreService
    {
        private IMongoCollection<TUser> _userCollections => AppStorage.GetCollection<TUser>(CollectionsConst.Users);

        /// <summary>
        /// 保存用户信息，如果已经存在则直接返回用户信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <created>tanch,2021/4/5</created>
        /// <changed>tanch,2021/4/5</changed>
        public async Task<UserInfoOutput> SaveUserAsync(UserSaveInput input)
        {
            var userInfo = await _userCollections.Find(a => a.WechatOpenId == input.OpenId).FirstOrDefaultAsync();
            if (userInfo==null)
            {
                userInfo = new TUser()
                {
                    HeadPic=input.HeadPic,
                    Name=input.Name,
                    WechatOpenId=input.OpenId,
                    Token = Guid.NewGuid().ToString("n"),
                    Sex=input.Sex
                };
                _userCollections.InsertOne(userInfo);
            }
            return new UserInfoOutput() 
            {
                HeadPic= userInfo.HeadPic,
                Name= userInfo.Name,
                UserId= userInfo.Id,
                Token = userInfo.Token,
            };
        }

        
        /// <summary>
        /// 根据token获取用户信息
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        /// <created>tanch,2021/4/5</created>
        /// <changed>tanch,2021/4/5</changed>
        public async Task<UserInfoOutput> GetUserByTokenAsync(string token)
        {
            var userInfo = await _userCollections.Find(a => a.Token == token).FirstOrDefaultAsync();
            if (userInfo!=null)
            {
                return new UserInfoOutput()
                {
                    HeadPic = userInfo.HeadPic,
                    Name = userInfo.Name,
                    UserId = userInfo.Id,
                    Token = userInfo.Token,
                };
            }
            return null;
        }

        public async Task<PageOutput<UserInfoOutput>> GetUsers(PageInput input)
        {

            var query = _userCollections.Find(a => true)
                .SortByDescending(a => a.CreateTime);
            var total = query.CountDocuments();
            var data = await query.Skip(input.Index).Limit(input.Size).ToListAsync();

            return new PageOutput<UserInfoOutput>()
            {
                Total = total,
                Data = data.Select(item => new UserInfoOutput() 
                {
                    HeadPic = item.HeadPic,
                    Name = item.Name,
                    UserId = item.Id,
                    Token = item.Token,
                }).ToList()
            };
        }
    }
}
